package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessTrackRecordMapper businessTrackRecordMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 新增商机跟进记录
     * @param businessTrackVo 数据
     */
    @Override
    public void insertBusinessRecord(BusinessTrackVo businessTrackVo) {
        //获取当前登陆用户的名字
        String username = SecurityUtils.getUsername();
        //创建商机跟进实体对象
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);
        //设置用户名
        tbBusinessTrackRecord.setCreateBy(username);
        //设置时间
        tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());
        //调用Mapper层处理
        businessTrackRecordMapper.insertBusinessRecord(tbBusinessTrackRecord);
        //修改对应的商机数据
        TbBusiness tbBusiness = new TbBusiness();
        //设置当前ID
        BeanUtils.copyProperties(businessTrackVo, tbBusiness);
        tbBusiness.setId(businessTrackVo.getBusinessId());
        //修改
        businessMapper.updateTbBusiness(tbBusiness);
    }

    /**
     * 退回公海
     *
     * @param id     商机id
     * @param reason 原因type值
     */
    @Override
    public void fuckingGo(Long id, String reason) {
        //根据ID更新商机状态
        businessMapper.setTransfer(id,TbClue.StatusType.RECOVERY.getValue());
        //查询退回原因
        String cause = sysDictDataMapper.selectDictLabel("reasons_for_business_reporting", reason);
        //创建商机跟进对象
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        //设置商机ID
        tbBusinessTrackRecord.setBusinessId(id);
        //设置沟通纪要
        tbBusinessTrackRecord.setRecord(cause);
        //设置原因
        tbBusinessTrackRecord.setFalseReason(reason);
        //获取操作人和时间
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());
        //设置默认沟通状态为沟通  ("1"为接通)
        tbBusinessTrackRecord.setTrackStatus("1");
        //设置当前跟进记录的类型 ("0"是正常跟进状态  "1"是退回公海)
        tbBusinessTrackRecord.setType("1");
        //插入跟进记录
        businessTrackRecordMapper.insertBusinessRecord(tbBusinessTrackRecord);

    }

    /**
     * 查询商机跟进记录
     *
     * @param id 商机ID
     * @return 跟进记录
     */
    @Override
    public List<TbBusinessTrackRecord> findAll(Long id) {
        return businessTrackRecordMapper.findAll(id);
    }
}
